package com.autonavi.yunda.yunji.core.utils;

import com.autonavi.yunda.yunji.common.exception.AmapException;
import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Base64;
import java.util.List;
import java.util.Objects;

/**
 * @author cizhong.wcs
 * @date 2022/8/5 16:36
 */
@SuppressWarnings("unused")
public class TransformUtil {
    private TransformUtil() {
    }

    public static String toUpperCase(String value) {
        return value.toUpperCase();
    }

    public static String toLowerCase(String value) {
        return value.toLowerCase();
    }

    public static String md5DigestAsHex(String value) {
        return DigestUtils.md5DigestAsHex(value.getBytes());
    }

    public static String base64Encode(String value) {
        return new String(Base64.getEncoder().encode(value.getBytes()));
    }

    public static String base64Decode(String value) {
        return new String(Base64.getDecoder().decode(value.getBytes()));
    }

    public static String concatString(String first, String second) {
        return first + second;
    }

    public static String joinString(String delimiter, List<String> array) {
        return String.join(delimiter, array);
    }

    public static String parseFileToString(MultipartFile file) {
        if (Objects.isNull(file)) {
            throw new AmapException(AmapExceptionCode.COMMON_REQUEST_ERROR, "文件为空");
        }
        String lineTxt;
        StringBuilder fileString = new StringBuilder();
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
            while ((lineTxt = bufferedReader.readLine()) != null) {
                fileString.append(lineTxt);
            }
        } catch (IOException e) {
            throw new AmapException(AmapExceptionCode.COMMON_REQUEST_ERROR, "文件读取异常", e);
        }
        return fileString.toString();
    }

    public static Object parseExcelToList(MultipartFile file) {
        return ExcelUtil.excelParseToList(file);
    }
}
